﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Linq;

using System.IO;
using System.Drawing;
using System.Net;

namespace LastFmClient
{
    public class SimilarAtist
    {
        public SimilarAtist(XElement x)
        {
            XElement = x;
        }

        private XElement XElement { get; set; }

        //        <name>Megadeth</name>
        //<mbid>a9044915-8be3-4c7e-b11f-9e2d2ea0a91e</mbid>
        //<match>100</match>
        //<url>http://www.last.fm/music/Megadeth</url>
        //<image_small>http://userserve-ak.last.fm/serve/34/380126.jpg</image_small>
        //<image>http://userserve-ak.last.fm/serve/126/380126.jpg</image>
        //<streamable>1</streamable>

        public string Name
        {
            get
            {
                return XElement.Element("name").Value;
            }
        }

        public string MusicBrainzId
        {
            get
            {
                return XElement.Element("mbid").Value;
            }
        }

        public double Match
        {
            get
            {
                return Convert.ToDouble(XElement.Element("match").Value);
            }
        }

        public string Url
        {
            get
            {
                return XElement.Element("url").Value;
            }
        }

        public string ImageSmallUrl
        {
            get
            {
                return XElement.Element("image_small").Value;
            }
        }

        public string ImageUrl
        {
            get
            {
                return XElement.Element("image").Value;
            }
        }

        public bool Streamable
        {
            get
            {
                return Convert.ToInt32(XElement.Element("streamable").Value) == 1;
            }
        }

        public Image Image
        {
            get
            {
                Stream ImageStream = new WebClient().OpenRead(this.ImageUrl);
                return Image.FromStream(ImageStream);
            }
        }

        public Image ImageSmall
        {
            get
            {
                Stream ImageStream = new WebClient().OpenRead(this.ImageSmallUrl);
                return Image.FromStream(ImageStream);
            }
        }

        private Artist m_Artist;
        public Artist Artist
        {
            get
            {
                if (m_Artist == null)
                {
                    m_Artist = new Artist(this.Name);
                }

                return m_Artist;
            }
        }
    }
}
